package org.lottery.panel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

import org.cbg.until.TraslationDemoScrollBarUI;
import org.come.model.Shop;
import org.come.until.SrcollPanelUI;

import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.UIUtils;

public class LotteryIntegralMainPanel extends JPanel {

	private JLabel integralTypeLab; // 积分类型
	private JScrollPane scrollPane;
	private JPanel jPanel;

	// private LotteryIntegralGoodsJpanel[] dailyLoginGoodsJpanel = new
	// LotteryIntegralGoodsJpanel[31];

	public LotteryIntegralMainPanel() {
		// TODO Auto-generated constructor stub
		this.setPreferredSize(new Dimension(588, 478));
		this.setLayout(null);
		this.setOpaque(false);
		// 关闭按钮事件
		FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 88);
		offBtn.setBounds(588 - 25, 0, 25, 25);
		this.add(offBtn);

		// 申请列表滚动条
		scrollPane = new JScrollPane();
//		scrollPane.getVerticalScrollBar().setUI(new TraslationDemoScrollBarUI(scrollPane));
		scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
		scrollPane.getVerticalScrollBar().setUnitIncrement(30);
		scrollPane.getViewport().setOpaque(false);
		scrollPane.setOpaque(false);
		scrollPane.setBounds(50, 99, 512, 357);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		jPanel = new JPanel();
		jPanel.setOpaque(false);
		jPanel.setLayout(null);

		scrollPane.setViewportView(jPanel);
		this.add(scrollPane);
		scrollPane.updateUI();
		scrollPane.invalidate();
		scrollPane.validate();
		scrollPane.repaint();

		getIntegralTypeLab();

	}

	private ImageIcon iconBack;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		if (iconBack == null) {
			iconBack = new ImageIcon("inkImg/old/background/S81.png");
		}
		g.drawImage(iconBack.getImage(), 0, 0, 588, 478, this);

	}

	// 获取兑换积分物品的数据
	public void getShopIntegralData(List<Shop> shopList, String integralType, String integralNow) {
		Component[] components = jPanel.getComponents();
		if (components.length >= shopList.size()) {
			for (int i = 0; i < shopList.size(); i++) {
				LotteryIntegralGoodsJpanel component2 = (LotteryIntegralGoodsJpanel) jPanel.getComponent(i);
				component2.getShopData(shopList.get(i));
			}
			for (int i = shopList.size(); i < components.length; i++) {
				LotteryIntegralGoodsJpanel component2 = (LotteryIntegralGoodsJpanel) jPanel.getComponent(i);
				component2.clearWindow();
			}
			jPanel.setPreferredSize(new Dimension(656, 65 * (shopList.size() % 2 == 0 ? shopList.size() / 2 : shopList.size() / 2 + 1)));
		} else if (components.length < shopList.size()) {
			for (int i = 0; i < components.length; i++) {
				LotteryIntegralGoodsJpanel component2 = (LotteryIntegralGoodsJpanel) jPanel.getComponent(i);
				component2.getShopData(shopList.get(i));
			}
			for (int i = components.length; i < shopList.size(); i++) {
				int row = i / 2;
				int rank = i % 2;
				LotteryIntegralGoodsJpanel dailyLoginGoodsJpanel = new LotteryIntegralGoodsJpanel(shopList.get(i));
				dailyLoginGoodsJpanel.setBounds(rank * 259 + 0, row * 65 + 0, 250, 65);
				jPanel.add(dailyLoginGoodsJpanel);
			}
			jPanel.setPreferredSize(new Dimension(656, 65 * (shopList.size() % 2 == 0 ? shopList.size() / 2 : shopList.size() / 2 + 1)));
		}

		// for (int i = 0; i < shopList.size(); i++) {
		// int row = i/2;
		// int rank = i%2;
		// LotteryIntegralGoodsJpanel dailyLoginGoodsJpanel = new
		// LotteryIntegralGoodsJpanel(shopList.get(i));
		// dailyLoginGoodsJpanel.setBounds(rank*259+0, row * 65 +0 , 250, 65);
		// jPanel.add(dailyLoginGoodsJpanel);
		//
		// }
		scrollPane.updateUI();
		scrollPane.invalidate();
		scrollPane.validate();
		scrollPane.repaint();
		this.integralTypeLab.setText(integralType + ":" + integralNow);
	}

	public JScrollPane getScrollPane() {
		return scrollPane;
	}

	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public JLabel getIntegralTypeLab() {
		if (integralTypeLab == null) {
			integralTypeLab = new JLabel("积分类型：当前积分数");
			integralTypeLab.setBounds(0, 53, 550, 20);
			integralTypeLab.setHorizontalAlignment(SwingConstants.RIGHT);
			integralTypeLab.setFont(UIUtils.TEXT_FONT);
			integralTypeLab.setOpaque(false);
			integralTypeLab.setForeground(UIUtils.COLOR_Pack);
			this.add(integralTypeLab);
		}
		return integralTypeLab;
	}

	public void setIntegralTypeLab(JLabel integralTypeLab) {
		this.integralTypeLab = integralTypeLab;
	}

}
